package com.bbm.bbmanage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbm.bbmanage.entity.AppHttpCodeEnum;
import com.bbm.bbmanage.entity.HouseImg;
import com.bbm.bbmanage.entity.vo.HouseImgVo;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.mapper.HouseImgMapper;
import com.bbm.bbmanage.service.HouseImgService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (HouseImg)表服务实现类
 *
 * @author lionkliu
 * @since 2023-12-11 14:04:23
 */
@Service("houseImgService")
public class HouseImgServiceImpl extends ServiceImpl<HouseImgMapper, HouseImg> implements HouseImgService {

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        return ResponseResult.okResult(page(new Page<>(pageNum, pageSize)));
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(HouseImg houseImg) {
        return save(houseImg) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(HouseImg houseImg) {
        UpdateWrapper<HouseImg> updateWrapper = new UpdateWrapper();
        // TODO：
        return update(updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult getImagesVoByHouseId(Long id) {
        LambdaQueryWrapper<HouseImg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HouseImg::getHouseId, id);
        List<HouseImg> images = list(queryWrapper);
        List<HouseImgVo> list = images.stream().map(img -> {
            HouseImgVo houseImgVo = new HouseImgVo();
            houseImgVo.setName(String.valueOf(img.getId()))
                    .setUrl(img.getImg());
            return houseImgVo;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(list);
    }

}

